package com.example.a79273.shandianban.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.a79273.shandianban.DesignerInfoActivity;
import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.config.NetConfigYouRen;
import com.example.a79273.shandianban.model.YouRenBean;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 79273 on 2016/8/31.
 */
public class YouRenListAdapter extends BaseAdapter implements View.OnClickListener {
    private List<YouRenBean.DataBean.ListBean> mData;
    private LayoutInflater inflater;
    private Picasso picasso;
    private Context mContext;



    public YouRenListAdapter(Context context, List<YouRenBean.DataBean.ListBean> data) {
        mData = data;
        inflater = LayoutInflater.from(context);
        picasso = Picasso.with(context);
        mContext = context;
    }


    @Override
    public int getCount() {
        return mData == null ? 0 : (mData.size() + 1) / 2;
    }

    @Override
    public List<YouRenBean.DataBean.ListBean> getItem(int position) {
        List<YouRenBean.DataBean.ListBean> aPairOfData = new ArrayList<>();
        aPairOfData.add(mData.get(2 * position));
        int secondPosition = 2 * position + 1;
        if (secondPosition < mData.size()) {
            aPairOfData.add(mData.get(secondPosition));
        }
        return aPairOfData;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_youren, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        List<YouRenBean.DataBean.ListBean> item = getItem(position);
        if (item.size() == 1) {
            holder.fl02.setVisibility(View.GONE);
            holder.verticalLine.setVisibility(View.GONE);
        } else {
            holder.fl02.setVisibility(View.VISIBLE);
            holder.verticalLine.setVisibility(View.VISIBLE);

            YouRenBean.DataBean.ListBean bean02 = item.get(1);
            picasso.load(NetConfigYouRen.ROOT_URL + bean02.getHsrc()).into(holder.pic02_iv);
            holder.fans02_tv.setText("" + bean02.getFans());
            holder.designerName02_tv.setText(bean02.getName());
            holder.pic02_iv.setTag(bean02.getDesignerId());
            holder.pic02_iv.setOnClickListener(this);
        }

        YouRenBean.DataBean.ListBean bean01 = item.get(0);
        picasso.load(NetConfigYouRen.ROOT_URL + bean01.getHsrc()).into(holder.pic01_iv);
        holder.fans01_tv.setText("" + bean01.getFans());
        holder.designerName01_tv.setText(bean01.getName());

        holder.pic01_iv.setTag(bean01.getDesignerId());
        holder.pic01_iv.setOnClickListener(this);


        return convertView;
    }

    @Override
    public void onClick(View v) {
        if (v != null) {
            String designerId = (String) v.getTag();
            Intent intent = new Intent(mContext, DesignerInfoActivity.class);
            intent.putExtra("designerId", designerId);
            mContext.startActivity(intent);
        }
    }


    static class ViewHolder {
        private ImageView pic01_iv, pic02_iv;
        private TextView fans01_tv, fans02_tv, designerName01_tv, designerName02_tv;
        private FrameLayout fl01, fl02;
        private View verticalLine;

        public ViewHolder(View view) {
            fl01 = (FrameLayout) view.findViewById(R.id.fl_item_youren_image01);
            pic01_iv = (ImageView) fl01.findViewById(R.id.iv_item_item_youren_image);
            fans01_tv = (TextView) fl01.findViewById(R.id.tv_item_item_youren_fansCount);
            designerName01_tv = (TextView) fl01.findViewById(R.id.tv_item_item_youren_designerName);

            verticalLine = view.findViewById(R.id.verticalLine);

            fl02 = (FrameLayout) view.findViewById(R.id.fl_item_youren_image02);
            pic02_iv = (ImageView) fl02.findViewById(R.id.iv_item_item_youren_image);
            fans02_tv = (TextView) fl02.findViewById(R.id.tv_item_item_youren_fansCount);
            designerName02_tv = (TextView) fl02.findViewById(R.id.tv_item_item_youren_designerName);

        }
    }
}
